package com.ag.dgm.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ag.bean.TbAdviceType;
import com.ag.bean.TbDisposalAdvice;
import com.ag.bean.TbEvent;
import com.ag.bean.TbPlan;
import com.ag.bean.TbPlanStandard;
import com.ag.common.BaseAction;
import com.ag.common.EventUtil;
import com.ag.dgm.service.EventNameService;
import com.ag.dgm.service.IAdviceTypeService;
import com.ag.dgm.service.IDisposalAdviceService;

/**
 * 
 * @author dgm 处置建议列表
 * 
 */
@Controller("DGMdisposalAdviceAction")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class DisposalAdviceAction extends BaseAction {

	@Resource(name = "disposalAdviceService")
	private IDisposalAdviceService service;

	@Resource(name = "adviceTypeServiceImpl")
	private IAdviceTypeService typeService;
	private TbEvent tbEvent = new TbEvent();
	private List<TbAdviceType> types; // 处置建议类型的list

	private List<TbDisposalAdvice> tbDisposalAdviceList; // 处置建议的list

	private String atIdString; // 建议类型的id

	private String eventId; // 事件id（ 这些建议都是这个事件的)

	private String eventName;

	@Resource(name = "eventNameServiceImpl")
	private EventNameService eventNameService;

	private TbPlan tbPlan;

	private String eventLevel;

	private String result;

	private List<TbPlanStandard> planStandardList;

	private String planReason;

	// private String totalMessagesString;

	// private String selectPageString;
	// private String pageRow;

	//
	// public String getTotalMessagesString() {
	// return totalMessagesString;
	// }
	//
	// public void setTotalMessagesString(String totalMessagesString) {
	// this.totalMessagesString = totalMessagesString;
	// }

	// public String getSelectPageString() {
	// return selectPageString;
	// }
	//
	// public void setSelectPageString(String selectPageString) {
	// this.selectPageString = selectPageString;
	// }
	//
	// public String getPageRow() {
	// return pageRow;
	// }
	//
	// public void setPageRow(String pageRow) {
	// this.pageRow = pageRow;
	// }

	public String getPlanReason() {
		return planReason;
	}

	public void setPlanReason(String planReason) {
		this.planReason = planReason;
	}

	public List<TbPlanStandard> getPlanStandardList() {
		return planStandardList;
	}

	public void setPlanStandardList(List<TbPlanStandard> planStandardList) {
		this.planStandardList = planStandardList;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getEventLevel() {
		return eventLevel;
	}

	public void setEventLevel(String eventLevel) {
		this.eventLevel = eventLevel;
	}

	public TbPlan getTbPlan() {
		return tbPlan;
	}

	public void setTbPlan(TbPlan tbPlan) {
		this.tbPlan = tbPlan;
	}

	public String getEventId() {
		return eventId;
	}

	public String getEventName() {
		return eventName;
	}

	public void setEventName(String eventName) {
		this.eventName = eventName;
	}

	public EventNameService getEventNameService() {
		return eventNameService;
	}

	public void setEventNameService(EventNameService eventNameService) {
		this.eventNameService = eventNameService;
	}

	public void setEventId(String eventId) {
		this.eventId = eventId;
	}

	public List<TbAdviceType> getTypes() {
		return types;
	}

	public IAdviceTypeService getTypeService() {
		return typeService;
	}

	public void setTypeService(IAdviceTypeService typeService) {
		this.typeService = typeService;
	}

	public String getAtIdString() {
		return atIdString;
	}

	public void setAtIdString(String atIdString) {
		this.atIdString = atIdString;
	}

	public void setTypes(List<TbAdviceType> types) {
		this.types = types;
	}

	public List<TbDisposalAdvice> getTbDisposalAdviceList() {
		return tbDisposalAdviceList;
	}

	public IDisposalAdviceService getService() {
		return service;
	}

	public TbEvent getTbEvent() {
		return tbEvent;
	}

	public void setTbEvent(TbEvent tbEvent) {
		this.tbEvent = tbEvent;
	}

	public void setService(IDisposalAdviceService service) {
		this.service = service;
	}

	public void setTbDisposalAdviceList(
			List<TbDisposalAdvice> tbDisposalAdviceList) {
		this.tbDisposalAdviceList = tbDisposalAdviceList;
	}

	/*
	 * 查找AdviceType
	 */
	public String findTypes() {
		// pageRow = "10";
		eventId = findEventId();
		try {
			types = typeService.findAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "main";
	}

	/*
	 * 
	 * 得到当前tbEvent 从session中拿, 在baseAction中有了方法，
	 */
	public String findEventId() {
		String eidString = null;
		try {
			// eidString =
			// (String)com.ag.processing.ProcessingPlans.getCurrentEventId();
			System.out.println("这是findEventId_action方法");
			eidString = (String) this
					.getSessionAttribute(EventUtil.PROCESSING_EVENT_ID);
			System.out
					.println("这是findEventId_action方法,取得的eventId为" + eidString);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return eidString;
	}

	/*
	 * 通过Type找advices
	 */
	public String findAdviceByType() throws Exception {
		// int selectPageInt=Integer.valueOf(selectPageString);
		// int pageRowInt=Integer.valueOf(pageRow);
		// int currentNumInt=(selectPageInt-1)*pageRowInt; //字符串转成int型
		eventId = findEventId();
		System.out.println("这是findAdviceByType_action方法" + eventId);
		try {
			types = typeService.findAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (atIdString.equals("0000000000")) {
			tbDisposalAdviceList = service.findAdviceByAllType(eventId);
			eventName = eventNameService.gainName(eventId);
			System.out.println("8***********************" + eventName);
		} else if (eventId == null) {
			// 当没有事件时即错误时
		} else {
			tbDisposalAdviceList = service
					.findAdviceByType(eventId, atIdString);
		}
		// tbDepts = service.findDept(eventId);
		// tbDisposalAdviceList = service
		// .findALL(departmentId, currentNumInt, pageRowInt,eventId);
		// tbDepts=service.findDept();
		// totalMessagesString = service.totalMessage(departmentId,
		// currentNumInt, pageRowInt,eventId);
		// for(TbDisposalAdvice t:tbDisposalAdviceList)
		// System.out.println("******************************"+t.getTbUser().getUName());
		return "main";
	}

	/*
	 * 因为偷懒，把一些东西写在这里了，一个事件的预案的查询dgm
	 */
	public String queryEventPlan() {
		System.out.println("这是queryEventPlan_action方法");
		String eid = null;
		try {
			eid = this.findEventId();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(eid + "*************");
		try {
			// planIdAndName = service.queryEventPlan(eid);
			tbPlan = service.queryEventPlan(eid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "queryEventPlan";
	}

	/*
	 * 还是因为偷懒，把这些东西放这里，在预案中，把事件的等级更新dgm
	 */
	public String updateEventLevel() {
		System.out.println("这是updateEventLevel_action");
		String eid = this.findEventId();
		eventLevel = "100" + eventLevel;
		result = "成功";
		System.out.println("这是 ");
		try {
			service.updateEventLevel(eventLevel, eid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result = "失败";
			e.printStackTrace();
		}
		return "updateEventLevel";
	}

	/*
	 * 通过事件查到当前的预案标准 dgm
	 */
	public String queryEventPlanStandard() {
		result = "成功";
		System.out.println("这是queryEventPlanStandard_action方法");
		String eid = this.findEventId();
		try {
			planStandardList = service.queryEventPlanStandard(eid);
			for (TbPlanStandard t : planStandardList) {
				System.out.println(t.getPContent());
			}
		} catch (Exception e) {
			result = "失败";
			e.printStackTrace();
		}
		return "queryEventPlanStandard";
	}

	/*
	 * 通过事件查事件表里面的plan_reason字段
	 */
	public String queryPlanReason() {
		System.out.println("这是queryPlanReason_action方法");
		String eid = this.findEventId();
		result = "成功";
		try {
			planReason=service.queryPlanReason(eid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = "失败";
		}
		return "queryPlanReason";
	}
	
	/*
	 * 通过事件id，把事件相关的预案，预案原因和预案标准删除
	 */
	public String delPlanByEvent(){
		this.setResult("删除成功");
		System.out.println("这是delPlanByEvent_action");
		String eid = this.findEventId();
		try {
			service.delPlanByEvent(eid);
		} catch (Exception e) {
			this.setResult("删除失败，请重试");
			e.printStackTrace();
		}
		return "delPlanByEvent";
	}
}
